<template>
	<view class="">
		<view class="list" v-for="item in list" :key="item.CnlNum">
			<view class="list__center">
				<view class="list__center--name">
					<text>{{item.CnlName}}</text>
				</view>
				<view class="list__center--code">
					<text>点位号：</text>
					<text>{{item.CnlNum}}</text>
				</view>
				<view class="list__center--icon">
					<image src="@/static/theme/default/home/video.png" mode="" @click="goVideo(item)"></image>
					<text @click="goVideo(item)">点击查看监控</text>
				</view>
			</view>
			<view class="list__right">
				<!-- <view class="list__right--top">
					<view class="list__right--top-text" :class="!item.check ? 'left-active' : ''">关</view>
					<view class="" style="margin: 0 10rpx;">
						<u-switch v-model="item.check" @change="change(item)"></u-switch>
					</view>
					<view class="list__right--top-text" :class="item.check ? 'right-active' : ''">开</view>
				</view> -->
				<view class="list__right--top">
					<view class="circle" @click="change(item)">{{item.ButtonText}}</view>
				</view>
				<view class="list__right--bottom">
					<text v-show="!item.status">状态异常</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import _data from '@/common/_data.js'
	import _custom from '@/common/_custom.js'
	
	export default {
		data() {
			return {
				list: []
			}
		},
		onShow() {
			this.getRemoteGuardList();
		},
		onNavigationBarButtonTap() {
			uni.showLoading({
				title: '刷新中',
			})
			this.getRemoteGuardList(() => {
				setTimeout(() => {
					uni.hideLoading();
				},300)
			});
		},
		methods: {
			/**
			 * 门禁列表
			 */
			getRemoteGuardList(fn) {
				let that = this;
				_custom.getDataJava({
					apiname: 'getRemoteGuardList',
					success(res) {
						if(fn) fn()
						if(!res.length) return
						res.forEach(item => {
							that.$set(item,'status',true)
							that.$set(item,'check',false)
							that.getStatus(item.CnlNum,res => {
								let stat = res.d.Data[0].Stat;
								let val = res.d.Data[0].Val;
								let status = stat == 1;
								that.$set(item,'status',status)
								if(!status) val = 0;
								let check = val == 1;
								that.$set(item,'check',check)
							})
						})
						that.list = res;
					},
					fail() {
						uni.showToast({
							title: '获取列表数据失败',
							icon: 'none'
						})
					}
				})
			},
			/**
			 * 获取门禁状态
			 * @param {Object} v
			 * @param {Object} fn
			 */
			getStatus(v,fn) {
				uni.request({
					url: 'http://221.226.178.122:1883/ClientApiSvc.svc/GetCurCnlDataExt2',
					data: {
						minNum: v,
						maxNum: v
					},
					method: 'GET',
					dataType: 'json',
					success(res) {
						res.data.d = JSON.parse(res.data.d);
						fn(res.data)
					},
					fail() {
						uni.showToast({
							title: '获取门禁状态失败',
							icon: 'none'
						})
					}
				})
			},
			/**
			 * 设置门禁
			 * @param {Object} v1
			 * @param {Object} v2
			 */
			setStatus(v1,v2,fn,fnErr) {
				uni.request({
					url: 'http://221.226.178.122:1883/ClientApiSvc.svc/SendCnlCommand',
					data: {
						username: _data.localData('obscode'),
						password: '12345',
						cnlNum: v1,
						cnlVal: v2,
						seconds: 5
					},
					method: 'GET',
					dataType: 'json',
					success(res) {
						console.log(res)
						res.data.d = JSON.parse(res.data.d);
						fn(res.data)
					},
					fail() {
						fnErr()
					}
				})
			},
			/**
			 * 切换门禁状态
			 * @param {Object} v
			 */
			change(v) {
				if(!v.status) {
					uni.showToast({
						title: '状态异常无法操作',
						icon: 'none'
					})
					return
				}
				this.setStatus(v.CnlNum,'1',res => {
					if(res.d.Success) {
						uni.showToast({
							title: '操作成功',
							icon: 'none'
						})
						// #ifdef APP-PLUS
						setTimeout(() => {
							uni.navigateTo({
								url: '../monitor/video?data=' + v.IndexCode
							})
						},500)
						// #endif
					} else {
						uni.showToast({
							title: '操作失败',
							icon: 'none'
						})
					}
				},() => {
					uni.showToast({
						title: '接口错误',
						icon: 'none'
					})
				});
			},
			/**
			 * 跳转监控页
			 * @param {Object} v
			 */
			goVideo(v) {
				uni.navigateTo({
					url: '../monitor/video?data=' + v.IndexCode
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.list {
		height: 280rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 20rpx;
		box-sizing: border-box;
		background-color: #fff;
		
		&__center {
			flex: 1;
			
			&--name {
				font-size: 35rpx;
			}
			
			&--code {
				font-size: 28rpx;
				color: #aaa;
			}
			
			&--icon {
				display: flex;
				
				image {
					width: 50rpx;
					height: 50rpx;
					margin-right: 10rpx;
				}
				
				text {
					color: skyblue;
				}
			}
		}
		
		&__right {
			
			&--top {
				display: flex;
				align-items: center;
				
				&-text {
					color: #bfbfbf;
					font-weight: bold;
				}
			}
			
			&--bottom {
				font-size: 25rpx;
				color: #f5222d;
				text-align: center;
				margin-top: 10rpx;
				height: 40rpx;
			}
		}
	}
	
	.circle {
		width: 150rpx;
		height: 150rpx;
		border-radius: 50%;
		background-color: #4096ff;
		box-shadow: 0 0 10rpx 10rpx rgba(0, 0, 0, .1);
		margin-right: 20rpx;
		color: #fff;
		font-size: 45rpx;
		text-align: center;
		line-height: 150rpx;
	}
	
	.left-active {
		color: #f5222d;
	}
	
	.right-active {
		color: #1677ff;
	}
</style>
